//
//  AboutViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/12.
//

import UIKit

class AboutViewController: QIBaseViewController {
    
    private var titleArr = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "关于"
        
        addUI()
        getData()
    }
    
    private func getData() {
        QINetworkManager.requestJson(urlStr: "/Config/getPhone", method: .get, parameters: [:]) { json in
             print(json)
            let mobileStr = json.stringValue
            self.titleArr = ["版本检查", "官方联系电话   \(mobileStr)"]
            self.tableView.reloadData()
        }
    }
   
    private func addUI() {
        titleArr = ["版本检查", "官方联系电话   020-1651164648"]
        let infoDictionary: Dictionary = Bundle.main.infoDictionary!
        
        let imgView = UIImageView().then {
            $0.frame = CGRect(x: SCREEN_WIDTH / 2 - 39, y: NAV_HEIGHT + 35, width: 78, height: 78)
            $0.image = UIImage(named: "logo")
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
            $0.addRoundedCorners(withRadius: 39)
        }
        view.addSubview(imgView)
        
        let nameL = UILabel().then({
            $0.frame = CGRect(x: 0, y: imgView.bottom + 29, width: SCREEN_WIDTH, height: 20)
            $0.text = "月升\(infoDictionary["CFBundleDisplayName"] ?? "")"
            $0.textColor = Color333333
            $0.textAlignment = .center
            $0.font = SYSFONT(ofSize: 18)
        })
        view.addSubview(nameL)
        
        let versionL = UILabel().then({
            $0.frame = CGRect(x: 0, y: nameL.bottom + 10, width: SCREEN_WIDTH, height: 20)
            $0.text = "V\(infoDictionary["CFBundleShortVersionString"] ?? "1.0.0")"
            $0.textColor = Color333333
            $0.textAlignment = .center
            $0.font = SYSFONT(ofSize: 14)
        })
        view.addSubview(versionL)
        
        tableView.frame = CGRect(x: 16, y: versionL.bottom + 40, width: SCREEN_WIDTH - 32, height: 132)
        view.addSubview(tableView)
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        tableView.backgroundColor = .white
//        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.addRoundedCorners(withRadius: 8)
        tableView.addBorder(withWidth: 1, borderColor: ColorEEEEEE)
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()

}

extension AboutViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        titleArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        66
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.selectionStyle = .none
        if indexPath.row == 0 {
            cell.accessoryType = .disclosureIndicator
        }
        cell.textLabel?.text = titleArr[indexPath.row]
        cell.textLabel?.textColor = Color333333
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            
        } else {
            // 要复制的文本
            let textToCopy = "020-1651164648"
            // 获取共享剪贴板对象
            let pasteBoard = UIPasteboard.general
            pasteBoard.string = textToCopy
            ShowHUD(str: "已复制")
        }
    }
    
}
